package com.cassandra;

import java.io.UnsupportedEncodingException;

import org.apache.cassandra.service.Cassandra;
import org.apache.cassandra.service.ColumnOrSuperColumn;
import org.apache.cassandra.service.ColumnPath;
import org.apache.cassandra.service.ConsistencyLevel;
import org.apache.cassandra.service.InvalidRequestException;
import org.apache.cassandra.service.TimedOutException;
import org.apache.cassandra.service.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class CassandraTest2 {

	public void testSimple() {
		System.out.println("-------------------");
		TTransport tr = new TSocket("localhost", 9160);
		TProtocol proto = new TBinaryProtocol(tr);
		Cassandra.Client client = new Cassandra.Client(proto);
		try {
			tr.open();

			String key_user_id = "yahoo";

			// insert data
			long timestamp = System.currentTimeMillis();
			client.insert("Keyspace1", key_user_id, new ColumnPath("Standard1", null, "url".getBytes("UTF-8")),
					"http://gpcuster.cnblogs.com".getBytes("UTF-8"), timestamp, ConsistencyLevel.ONE);

			//	        client.insert("Keyspace1",
			//	                      key_user_id,
			//	                      new ColumnPath("Standard1", null, "作者".getBytes("UTF-8")),
			//	                      "逖靖寒".getBytes("UTF-8"),
			//	                      timestamp,
			//	                      ConsistencyLevel.ONE);

			// read single column
			ColumnPath path = new ColumnPath("Standard1", null, "url".getBytes("UTF-8"));
			ColumnOrSuperColumn column = client.get("Keyspace1", key_user_id, path, ConsistencyLevel.ONE);
			String value = new String(column.getColumn().getValue());
			System.out.println(value);

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (InvalidRequestException e) {
			e.printStackTrace();
		} catch (UnavailableException e) {
			e.printStackTrace();
		} catch (TimedOutException e) {
			e.printStackTrace();
		} catch (TException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			tr.close();
		}
		System.out.println("执行成功");
	}

	public static void main(String[] args) {
		CassandraTest2 test = new CassandraTest2();
		System.out.println("ddddddd");
		test.testSimple();
	}
}
